package com.dongdongshop.service.impl;

import com.dongdongshop.mapper.TbItemCatMapper;
import com.dongdongshop.page.PageResult;
import com.dongdongshop.pojo.TbItemCat;
import com.dongdongshop.pojo.TbItemCatExample;
import com.dongdongshop.service.TbItemCatService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;
import java.util.List;

@Service
@DubboService
public class TbItemCatServiceImpl implements TbItemCatService {
    @Autowired
    private TbItemCatMapper catMapper;

    @Override
    public List<TbItemCat> listCat(Long pid) {
        TbItemCatExample example = new TbItemCatExample();
        TbItemCatExample.Criteria criteria = example.createCriteria();
        criteria.andParentIdEqualTo(pid);
        List<TbItemCat> cats = catMapper.selectByExample(example);
        return cats;
    }

    @Override
    public void addCat(TbItemCat cat) {
        catMapper.insertSelective(cat);
    }

    @Override
    public TbItemCat selectCatById(Long id) {
        return catMapper.selectByPrimaryKey(id);
    }

    @Override
    public void updateById(TbItemCat cat) {
        catMapper.updateByPrimaryKeySelective(cat);
    }

    @Override
    public void deleteCats(Long[] ids) {
        TbItemCatExample example = new TbItemCatExample();
        TbItemCatExample.Criteria criteria = example.createCriteria();
        criteria.andIdIn(Arrays.asList(ids));
        List<TbItemCat> tbItemCats = catMapper.selectByExample(example);
        deleteCatByIds(tbItemCats);
    }

    @Override
    public Integer deleteCatss(Long[] ids) {
        TbItemCatExample example = new TbItemCatExample();
        TbItemCatExample.Criteria criteria = example.createCriteria();
        criteria.andIdIn(Arrays.asList(ids));
        List<TbItemCat> tbItemCats = catMapper.selectByExample(example);
        for (TbItemCat cat : tbItemCats) {
            TbItemCatExample example2 = new TbItemCatExample();
            TbItemCatExample.Criteria criteria2 = example2.createCriteria();
            criteria2.andParentIdEqualTo(cat.getId());
            List<TbItemCat> tbItemCats2 = catMapper.selectByExample(example2);
            if(tbItemCats2.size() > 0){
                return 1;
            }else {
                catMapper.deleteByPrimaryKey(cat.getId());
            }
        }
        return 0;
    }

    public void deleteCatByIds(List<TbItemCat> catList){
        if(catList.size() > 0){
            for (TbItemCat cat : catList) {
                catMapper.deleteByPrimaryKey(cat.getId());
                TbItemCatExample example = new TbItemCatExample();
                TbItemCatExample.Criteria criteria = example.createCriteria();
                criteria.andParentIdEqualTo(cat.getId());
                List<TbItemCat> tbItemCats = catMapper.selectByExample(example);
                deleteCatByIds(tbItemCats);
            }
        }else{
            return;
        }

    }

}
